import frappe
from frappe.model.document import Document
from frappe.model.docstatus import DocStatus
from frappe import _

from frappe import new_doc
from frappe import session
import json
from frappe.desk.doctype.notification_log.notification_log import (
    enqueue_create_notification,
    get_title,
    get_title_html,
)

@frappe.whitelist()
def 当前角色():
    角色=frappe.get_roles(frappe.session.user)
    return 角色

def 当前用户():
    user_name = frappe.get_cached_value("User", frappe.session.user, "full_name")
    return user_name


@frappe.whitelist()
def 发送通知(doc_type, doc_name, 通知用户组, description=None):
    # for 通知用户 in frappe.parse_json(args.get("assign_to")):
    for 通知用户 in frappe.parse_json(通知用户组):
        语言设置 = frappe.db.get_value("User", 通知用户, ["language", "enabled"], as_dict=True)
        if not 语言设置:
            return "发送通知 出错"
        # return if self assigned or user disabled
        # if assigned_by == 通知用户 or not assigned_user.enabled:
        #     return

        user_name = 当前用户()
        description_html = f"<div>{description}</div>" if description else None

        doc = frappe.get_doc(doc_type, doc_name)
        subject = doc.流程提醒
        user_name = frappe.bold(user_name)
        # document_type = frappe.bold(_(doc_type, lang=assigned_user.language))
        subject = _(subject, lang=语言设置.language)

        notification_doc = {
            "type": "Alert",
            "document_type": doc_type,
            "subject": subject,
            "document_name": doc_name,
            "from_user": frappe.session.user,
            "email_content": description_html,
        }
        # frappe.msgprint(notification_doc, "推送消息给用户")
        print(f"通知用户 👉 {notification_doc}")
        print(f"通知用户 👉 {通知用户}")
        enqueue_create_notification(通知用户, notification_doc)
    return "ok"


@frappe.whitelist()
def 查物料需求单(物料需求单="", 其他参数=""):
    print(f"物料需求单 👉 {物料需求单}")
    print(f"其他参数 👉 {其他参数}")
    doc = frappe.get_doc("Material Request", 物料需求单)
    print(f"doc 👉 {doc}")
    return doc


@frappe.whitelist()
def 更新lczz(id, 物料需求单="", 采购单="", 其他参数=""):
    # print(f"接收到的arg的类型是: {type(arg)}，值是: {arg}")
    # arg = json.loads(arg)
    # print(f"json后 arg的类型是: {type(arg)}，值是: {arg}")
    print(f"id 👉 {id}")
    print(f"物料需求单 👉 {物料需求单}")
    print(f"其他参数 👉 {其他参数}")
    doc = frappe.get_doc("lczz", id)
    if 物料需求单:
        doc.物料需求单 = 物料需求单
    if 采购单:
        doc.采购单 = 采购单
    msg = doc.前进一步()
    py发送通知(doc, [frappe.session.user, "ywy1@qq.com"])
    return msg


def py发送通知(doc, 通知用户组, description=None):
    # for 通知用户 in frappe.parse_json(args.get("assign_to")):
    for 通知用户 in frappe.parse_json(通知用户组):
        语言设置 = frappe.db.get_value("User", 通知用户, ["language", "enabled"], as_dict=True)
        if not 语言设置:
            return "py发送通知 出错"
        # return if self assigned or user disabled
        # if assigned_by == 通知用户 or not assigned_user.enabled:
        #     return

        user_name = 当前用户()
        description_html = f"<div>{description}</div>" if description else None

        # doc = frappe.get_doc(doc_type, doc_name)
        subject = doc.流程提醒
        user_name = frappe.bold(user_name)
        # document_type = frappe.bold(_(doc_type, lang=assigned_user.language))
        subject = _(subject, lang=语言设置.language)

        notification_doc = {
            "type": "Alert",
            "document_type": doc.doctype,
            "subject": subject,
            "document_name": doc.name,
            "from_user": frappe.session.user,
            "email_content": description_html,
        }
        # frappe.msgprint(notification_doc, "推送消息给用户")
        print(f"通知用户 👉 {notification_doc}")
        print(f"通知用户 👉 {通知用户}")
        enqueue_create_notification(通知用户, notification_doc)
    return "ok"


def cs2():
    # communication = frappe.new_doc("Communication")
    # communication.communication_type = "Notification"
    # communication.communication_medium = "Event"
    # communication.subject = _("流程追踪单")

    # msg = _(f"流程追踪单:创建成功! 点击 <a href=''>这里</a> 查看")
    # communication.content = msg
    # # 获取当前登录用户并添加到收件人列表
    # communication.recipients = f"{session.user},ywy1@qq.com,业务员1"
    # communication.insert()

    notification = frappe.new_doc("Notification")
    notification.name = "ToDo Status Update"
    notification.subject = "ToDo Status Update"
    notification.document_type = "ToDo"
    notification.event = "老鱼通知"
    notification.value_changed = "status"
    notification.send_to_all_assignees = 1
    notification.set_property_after_alert = "description"
    notification.property_value = "Changed by Notification"
    notification.save()


def cs():
    print("测试成功")
